package com.atguigu.tingshu.live.api;

import com.atguigu.tingshu.common.login.EnableLoginAuth;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.live.service.LiveRoomService;
import com.atguigu.tingshu.model.live.LiveRoom;
import com.atguigu.tingshu.vo.live.LiveRoomVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/live/liveRoom")
@SuppressWarnings({"unchecked", "rawtypes"})
public class LiveRoomApiController {

    @Autowired
    private LiveRoomService liveRoomService;

    @EnableLoginAuth
    @GetMapping("/getCurrentLive")
    public Result<LiveRoom> getCurrentLive() {
        LiveRoom liveRoom = liveRoomService.getCurrentLive();
        return Result.ok(liveRoom);
    }

    @EnableLoginAuth
    @PostMapping("/saveLiveRoom")
    public Result<LiveRoom> saveLiveRoom(@RequestBody LiveRoomVo liveRoomVo) {
        LiveRoom liveRoom = liveRoomService.saveLiveRoom(liveRoomVo);
        return Result.ok(liveRoom);
    }

    @GetMapping("/getById/{id}")
    public Result<LiveRoom> getById(@PathVariable Long id) {
        LiveRoom liveRoom = liveRoomService.getById(id);
        return Result.ok(liveRoom);
    }

    @GetMapping("/findLiveList")
    public Result<List<LiveRoom>> findLiveList() {
        List<LiveRoom> roomList = liveRoomService.list(new LambdaQueryWrapper<LiveRoom>().gt(LiveRoom::getExpireTime, new Date()));
        return Result.ok(roomList);
    }
}

